package com.example.logicalthinking.broadcastbestpracice;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BaseaActivity implements View.OnClickListener{

    private EditText accountText;
    private EditText passwordText;
    private Button login;
    private SharedPreferences pref; //取数据
    private SharedPreferences.Editor editor; //编辑数据，写入数据
    private CheckBox rememberPsw;
    public static final int UPDATE_TEXT = 1;
    private TextView threadText;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATE_TEXT:

                    threadText.setText("Nice to meet you");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        accountText = (EditText) findViewById(R.id.account);
        passwordText = (EditText) findViewById(R.id.password);

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        rememberPsw = (CheckBox) findViewById(R.id.remember_psw);
        boolean isremember = pref.getBoolean("remember_psw", false);
        if (isremember){
            String account = pref.getString("account", "");
            String password = pref.getString("password", "");
            accountText.setText(account);
            passwordText.setText(password);
            rememberPsw.setChecked(true);
        }
        threadText = (TextView) findViewById(R.id.thread_text);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);

        Button web = (Button) findViewById(R.id.webview_activity);
        web.setOnClickListener(this);

        Button net = (Button) findViewById(R.id.network_activity);
        net.setOnClickListener(this);

        Button thread = (Button) findViewById(R.id.thread);
        thread.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login: {
                String account = accountText.getText().toString();
                String password = passwordText.getText().toString();

                //如果账号是admin 且密码是123456 就认为登录成功
                if (account.equals("admin") && password.equals("123456")) {
                    editor = pref.edit();
                    if (rememberPsw.isChecked()) {
                        editor.putString("account", account);
                        editor.putString("password", password);
                        editor.putBoolean("remember_psw", true);
                    } else {
                        editor.clear();

                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("用户名或密码错误")
                            .setMessage("用户名应为admin，密码应为123456")
                            .setCancelable(true)
                            .setNegativeButton("取消", null)
                            .create()
                            .show();
                    //                        AlertDialog.Builder dialog = new AlertDialog.Builder(LoginActivity.this);
//                        dialog.setTitle("恭喜你，中奖了！！！");
//                        dialog.setMessage("");
//                        dialog.setCancelable(false);
//                        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                AlertDialog.Builder pig = new AlertDialog.Builder(LoginActivity.this);
//                                pig.setTitle("被骗了吧，笨蛋！");
//                                pig.setMessage("哈哈哈");
//                                pig.setCancelable(true);
//                                pig.setPositiveButton("开奖", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        ProgressDialog proDia = new ProgressDialog(LoginActivity.this);
//                                        proDia.setTitle("领奖进行时");
//                                        proDia.setMessage("嘿嘿，这是一个无限循环");
//                                        proDia.setCancelable(true);
//                                        proDia.show();
//                                    }
//                                });
//                                pig.show();
//                            }
//                        });
                }
            }
                break;
            case R.id.webview_activity:{
                Intent intent = new Intent(LoginActivity.this, WebViewActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.network_activity:{
                Intent intent = new Intent(LoginActivity.this, NetworkActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.thread:{
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = UPDATE_TEXT;
                        handler.sendMessage(message);
                    }
                }).start();

            }
            break;
        }
    }


}
